package org.hackystat.ui.sensordatabrowser.server;

import java.math.BigInteger;

import javax.xml.datatype.XMLGregorianCalendar;

import org.hackystat.sensorbase.resource.projects.jaxb.SensorDataSummary;
import org.hackystat.ui.sensordatabrowser.client.common.Event;

/**
 * This class represents one event of sensor data elements in the UI display, an event is
 * quantized to the the sensor data types occurring within one minute that have the same 
 * type and tool attributes.
 * @author David J. Nickles
 */
public class EventHandler {
  
  /**
   * Makes a new Event with the properties from SensorDataSummary.
   * @param timestamp the event timestamp.
   * @param dataSummary a sensor data summary element.
   * @return Event representing the specified data.
   */
  public static Event makeEvent(XMLGregorianCalendar timestamp, SensorDataSummary dataSummary) {
    BigInteger count = dataSummary.getNumInstances();
    String type = dataSummary.getSensorDataType();
    String tool = dataSummary.getTool();
    String xmlTimestamp = SimpleDateFactories.getInstance().getXmlfactory().format(
      timestamp.toGregorianCalendar().getTime());
    xmlTimestamp = xmlTimestamp.substring(0, xmlTimestamp.length() - 2) + ":"
      + xmlTimestamp.substring(xmlTimestamp.length() - 2, xmlTimestamp.length());
    String headerTstamp = SimpleDateFactories.getInstance().getHeaderfactory().format(
      timestamp.toGregorianCalendar().getTime());
    String title = headerTstamp + " " + type + " " + tool + "[" + count + "]";
    return new Event(xmlTimestamp, type, tool, title, count.intValue());
  }
}